package com.wiscamp.ninechapters.challenges.adapter.resources;

import com.wiscamp.ninechapters.challenges.application.ChallengesApplicationService;
import com.wiscamp.ninechapters.challenges.application.dto.ChallengeReportDTO;
import com.wiscamp.ninechapters.challenges.domain.models.*;
import com.wiscamp.ninechapters.challenges.domain.services.GroupsService;
import com.wiscamp.ninechapters.challenges.domain.services.MedalsService;
import com.wiscamp.ninechapters.challenges.domain.services.PointsService;
import cube.ddd.adapter.RestResponse;
import cube.ddd.aspect.AopResourceException;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 数学大作战-大作战服务端点
 *
 * @author yifeng
 */
@RequestMapping("/")
@Tag(name = "数学竞赛-数学大作战")
@RestController
public class ChallengeResource {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PointsService pointService;

    @Autowired
    private GroupsService groupService;

    @Autowired
    private MedalsService medalService;

    @Autowired
    private ChallengesApplicationService challengesApplicationService;

    /*@Autowired
    private AssignmentTaskService assignmentTaskEntityService;*/

    public ChallengeResource(PointsService pointService, GroupsService groupService, MedalsService medalService, ChallengesApplicationService challengesApplicationService) {
        this.pointService = pointService;
        this.groupService = groupService;
        this.medalService = medalService;
        this.challengesApplicationService = challengesApplicationService;
        //this.assignmentTaskEntityService = assignmentTaskEntityService;
    }

    @GetMapping("battle/users/{userId}/points")
    @Operation(summary = "获取用户积分及排名")
    @AopResourceException
    public RestResponse<UserPoints> getUserPoints(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, pointService::getUserPoints);
    }

    @GetMapping("battle/users/{userId}/Group/Points")
    @Operation(summary = "获取用户所在团队及排名")
    @AopResourceException
    public RestResponse<UserGroup> getGroupPoints(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, groupService::getGroupByMember);
    }

    @GetMapping("battle/users/{userId}/medals")
    @Operation(summary = "获取用户所获取成就勋章列表")
    @AopResourceException
    public RestResponse<Collection<Medal>> getUserMedals(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId) {
        return ResourceUtils.getListById(request, response, userId, medalService::getUserMedals);
    }

    @GetMapping("battle/users/{userId}/challenge/{grade}")
    @Operation(summary = "获取本日挑战任务编号")
    @AopResourceException
    public RestResponse<Long> getBattleTask(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId, @PathVariable("grade") int grade) {
        //var result = assignmentTaskEntityService.getAssignmentTaskOfDailyChallenge(userId, grade);
        return ResourceUtils.getOkResponse(request, response, null);
    }

    @GetMapping("battle/users/{userId}/challenge/{grade}/completed")
    @Operation(summary = "判断用户是否完成了今天的挑战任务")
    @AopResourceException
    public RestResponse<Boolean> completedBattleTask(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId, @PathVariable("grade") int grade) {
        // var result = assignmentTaskEntityService.isCompletedTaskOfDailyChallenge(userId, grade);
        return ResourceUtils.getOkResponse(request, response, null);
    }

    @GetMapping("battle/users/{userId}/tasks/{taskId}/report")
    @Operation(summary = "获取每日挑战任务简报")
    @AopResourceException
    public RestResponse<ChallengeReportDTO> getBattleReport(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("taskId") long taskId) {
        return null;
        //return ResourceUtils.getById(request, response, taskId, logger, challengesApplicationService::getBattleReport);
    }

    @GetMapping("battle/ranks/personal/week")
    @Operation(summary = "获取个人排名周榜")
    @AopResourceException
    public RestResponse<Collection<UserPointsWeek>> getUserPointsWeek(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, pointService::getTop100UsersPointsWeek);
    }

    @GetMapping("battle/ranks/personal/month")
    @Operation(summary = "获取个人排名月榜")
    @AopResourceException
    public RestResponse<Collection<UserPointsMonth>> getUserPointsMonth(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, pointService::getTop100UsersPointsMonth);
    }

    @GetMapping("battle/ranks/personal/total")
    @Operation(summary = "获取个人排名总榜")
    @AopResourceException
    public RestResponse<Collection<UserPoints>> getUserPoints(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, pointService::getTop100UsersPoints);
    }

    @GetMapping("battle/ranks/group/total")
    @Operation(summary = "获取团队排名总榜")
    @AopResourceException
    public RestResponse<Collection<UserGroup>> getGroupPoints(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, groupService::getTop100GroupBySortedPoints);
    }

    @PostMapping("battle/invite/help")
    @Operation(summary = "发送邀请助力请求")
    @AopResourceException
    public RestResponse<InviteHelp> createInviteHelp(@Context HttpServletRequest request, @Context HttpServletResponse response, InviteHelp inviteHelp) {
        return ResourceUtils.createOrUpdate(request, response, inviteHelp, challengesApplicationService::createInviteHelp);
    }

    @GetMapping("battle/users/{userId}/invite/helps")
    @Operation(summary = "获取今天助力的朋友清单")
    @AopResourceException
    public RestResponse<Collection<InviteHelp>> getInvitedHelps(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId) {
        return ResourceUtils.getListById(request, response, userId, groupService::getInviteHelps);
    }

    @PostMapping("battle/users/{userId}/group")
    @Operation(summary = "创建组队")
    @AopResourceException
    public RestResponse<UserGroup> createUserGroup(@Context HttpServletRequest request, @Context HttpServletResponse response, UserGroup group) {
        return ResourceUtils.createOrUpdate(request, response, group, challengesApplicationService::createUserGroup);
    }

    @GetMapping("battle/users/{userId}/group/users")
    @Operation(summary = "获取本组成员名单")
    @AopResourceException
    public RestResponse<Collection<GroupUser>> getGroupUsers(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId) {
        return ResourceUtils.getListById(request, response, userId, groupService::getGroupUsers);
    }

    @PostMapping("battle/groups/bind/{userId}/{memberId}")
    @Operation(summary = "发送邀请组队请求")
    @AopResourceException
    public RestResponse<GroupUser> groupingInvite(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId, @PathVariable("memberId") long memberId) {
        var result = groupService.groupingInvite(userId, memberId);
        return ResourceUtils.getOkResponse(request, response, result);
    }
}
